package com.zzvcom.webapp.action.web;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.zzvcom.persist.model.admin.Faq;
import com.zzvcom.service.admin.FaqManager;
import com.zzvcom.template.ResultPage;
import com.zzvcom.utils.DateUtil;

@Controller
@Scope("prototype")
public class FaqViewAction
{

    private Faq faq;

    private FaqManager manager;

    private ResultPage resultPage;

    private String param;

    public String execute()
    {
        if (resultPage == null)
        {
            resultPage = new ResultPage();
        }
        if (StringUtils.isNotBlank(param))
        {
            resultPage.setCurrentPage(Integer.valueOf(param));
        }
        DetachedCriteria dc = DetachedCriteria.forClass(Faq.class);
        if (faq != null && StringUtils.isNotBlank(faq.getQuestion()))
        {
            dc.add(Restrictions.like("question", faq.getQuestion(),
                    MatchMode.START));
        }
        dc.add(Restrictions.eq("display", "0"));
        dc.addOrder(Order.asc("seq"));        
        resultPage.setDetachedCriteria(dc);
        manager.getResultPageByDetachedCriteria(resultPage);
        return "faq";
    }

    public String p()
    {
        return execute();
    }

    public void  add()
    {
        if (faq == null || StringUtils.isBlank(faq.getQuestion()))
        {
            return ;
        }
        if (StringUtils.isBlank(faq.getAsker()))
        {
            faq.setAsker("anonymous");
        }
        faq.setAddtime(DateUtil.dateToNum14(new Date()));
        faq.setDisplay("1");
        faq.setSeq(0);
        manager.save(faq);       
    }

    private Faq preFaq;

    private Faq nextFaq;

    public String a()
    {
        faq = manager.get(param);
        if (faq != null)
        {
            DetachedCriteria preDC = DetachedCriteria.forClass(Faq.class);
            preDC.add(Restrictions.eq("display", "0"));
            preDC.addOrder(Order.asc("seq"));           
            preDC.add(Restrictions.le("seq", faq.getSeq())); 
            preDC.add(Restrictions.ne("id", faq.getId())); 
            List<Faq> list = manager.getListByCriteria(preDC, 0, 1);
            if (list.size() > 0)
            {
                preFaq = list.get(0);
            }

            DetachedCriteria nextDC = DetachedCriteria.forClass(Faq.class);
            nextDC.add(Restrictions.eq("display", "0"));
            nextDC.addOrder(Order.asc("seq"));           
            nextDC.add(Restrictions.ge("seq", faq.getSeq()));
            nextDC.add(Restrictions.ne("id", faq.getId())); 
            list = manager.getListByCriteria(nextDC, 0, 1);
            if (list.size() > 0)
            {
                nextFaq = list.get(0);
            }

        }

        return "afaq";
    }

    public Faq getFaq()
    {
        return faq;
    }

    public void setFaq(Faq faq)
    {
        this.faq = faq;
    }

    public FaqManager getManager()
    {
        return manager;
    }

    @Autowired
    public void setManager(FaqManager manager)
    {
        this.manager = manager;
    }

    public ResultPage getResultPage()
    {
        return resultPage;
    }

    public void setResultPage(ResultPage resultPage)
    {
        this.resultPage = resultPage;
    }

    public String getParam()
    {
        return param;
    }

    public void setParam(String param)
    {
        this.param = param;
    }

    public Faq getPreFaq()
    {
        return preFaq;
    }

    public void setPreFaq(Faq preFaq)
    {
        this.preFaq = preFaq;
    }

    public Faq getNextFaq()
    {
        return nextFaq;
    }

    public void setNextFaq(Faq nextFaq)
    {
        this.nextFaq = nextFaq;
    }

}
